<?php
namespace Home\Controller;

/**
 * 地理位置ajax接口
 * @modify trainyao 2016-01-04 创建
 */

class PositionController extends \Home\Controller\ParentController
{
    protected function _initialize() {
        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 获取区数据
     * @modify trainyao 2016-01-04    创建
     * @modify trainyao 2017-06-08    多语言
     */
    public function getDistrict() {
        $param = I('post.');

        $validateRule = array(
            'city' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $cityId = $param['city'];
        $districtModel = D('District');

        $districtData = $districtModel->getInfoByCity($cityId);
        // 选择语言
        foreach ($districtData as $key => &$districtDataItem) {
            switch (LANG_SET) {
                case 'zh_cn':
                    $districtDataItem['name'] = explode('/', $districtDataItem['name'])[0];
                    break;
                case 'en_us':
                    $districtDataItem['name'] = explode('/', $districtDataItem['name'])[1];
                    break;
                case 'zh_tw':
                    $districtDataItem['name'] = explode('/', $districtDataItem['name'])[2];
                    break;
                default:
                    $districtDataItem['name'] = explode('/', $districtDataItem['name'])[0];
                    break;
            }
        }
        unset($districtDataItem);

        if(empty($districtData)) {
            returnJson(500, 512, 'error');
        }

        returnJson(200, 200, $districtData);
    }
}